class RelationController < ApplicationController
  def index
    @people = Person.find(:all)
  end

  def edit
    @selected_person = Person.find(params[:id]);
    @my_locations_array = Location.find(:all, :joins => [:people], :conditions => {:people => {:id => params[:id]}}).collect{|loc| loc.id}
    @all_locations = Location.find(:all)
  end

  def update
    @selected_person = Person.find(params[:id]);
    selected_locations = params[:selected]
    my_locations = PersonToLocationRelation.find(:all, :conditions => {:person_id => params[:id]}).collect{|loc| loc.location_id}

    PersonToLocationRelation.destroy_all( "person_id = " + params[:id] + " AND location_id NOT IN ('" + selected_locations.join("', '") + "')" )

    selected_locations.each do |location|
      new = PersonToLocationRelation.new
      new.insert(params[:id], location)
    end

    redirect_to "/relation"
  end

end
